<?php
namespace app\shops\controller;

use app\store\controller\Weixin;
use think\Db;
use think\Model;

/**
 * 门店管理
 */
class Store extends Base
{
    /**
     * @throws
     * 门店列表
     * @return [type] [description]
     */
    public function index()
    {
        $list = Db("store")->where('shop_id',$this->shop_id)->select();
        $this->assign('list',$list);

        return $this->fetch();
    }


    /**
     * 添加门店
     */
    public function add()
    {
        // 商户信息
        $shop_info = $this->shop_info($this->shop_id);

        // 已开门店数
        $store_count = Db("store")->where('shop_id',$this->shop_id)->count();

        if(request()->isPost()){

            if($store_count>=$shop_info['can_open_store']){
                $this->error('门店数额已达上线');
            }

            $data['store_no']       = input('store_no');
            $data['store_name']     = input('store_name');
            $data['store_address']  = input('store_address');
            $data['store_tel']      = input('store_tel');
            $data['shop_id']        = $this->shop_id;
            $data['remark']         = input('remark');

            $re = Db("store")->insert($data);
            if($re){
                // 门店数量增加
                Db("shops")->where('shop_id',$this->shop_id)->setInc('al_open_store',1);

                $this->success("添加成功");
            }else{
                $this->error("网络错误");
            }

        }

        if($shop_info['al_open_store'] == 0){
            $store_no = 101;
        }else{

            // 检索目前最大编号
            $store_no_max   = Db("store")->where('shop_id',$this->shop_id)->max('store_no');
            $store_no       = $store_no_max+1;
        }

        $this->assign('store_no',$store_no);

        return $this->fetch();
    }


    /**
     * @throws
     * 编辑门店
     */
    public function edit()
    {
        $store_id = input('store_id');

        // 门店详情
        $store_info = Db("store")->where('store_id',$store_id)->where('shop_id',$this->shop_id)->find();

        $this->assign('store_info',$store_info);

        // 更新数据
        if(request()->isPost()){

            input('store_name')     == null || $data['store_name']     = input('store_name');
            input('store_address')  == null || $data['store_address']  = input('store_address');
            input('store_tel')      == null || $data['store_tel']      = input('store_tel');
            input('remark')         == null || $data['remark']         = input('remark');


            $re = Db("store")->where('shop_id',$this->shop_id)->where('store_id',$store_id)->update($data);
            if($re===false){
                $this->error("编辑失败");
            }
            $this->success("编辑成功");

        }

        return $this->fetch();
    }


    /**
     * 绑定管理员
     */
    public function bind()
    {
        // 门店id
        $store_id = input('store_id');
        $this->assign('store_id',$store_id);

        // 微信获取授权地址
        $wx_request_url = request()->domain() . url("wchat/oauth/getwxinfo");

        // 回调地址
        // 门店管理员
        $param_d['shop_id']= $this->shop_id;
//        $param_d['shop_id']= $this->shop_id;
        $param_d['store_id']= $store_id;
        $param_d['type']= 'bind_admin';
        $admin_call_bacl_url    = request()->domain() . url("store/manager.index/admin_wxinfo",$param_d);
        // 门店签到员
        $param_s['shop_id']= $this->shop_id;
//        $param_d['shop_id']= $this->shop_id;
        $param_s['store_id']= $store_id;
        $param_s['type']= 'bind_sign_id';
        $sign_call_bacl_url     = request()->domain() . url("store/checker.index/bind_sign",$param_s);
        // 绑定教师
        $param_t['shop_id']= $this->shop_id;
//        $param_d['shop_id']= $this->shop_id;
        $param_t['store_id']= $store_id;
        $param_t['type']= 'bind_teacher';
        $teacher_call_bacl_url  = request()->domain() . url("store/teacher.index/hand_wxinfo",$param_t);


        // 门店管理员
        $param_p['shop_id']= $this->shop_id;
//        $param_d['shop_id']= $this->shop_id;
        $param_p['store_id']= $store_id;
        $param_p['type']= 'bind_admin';
        $parent_call_bacl_url    = request()->domain() . url("store/parent.index/parent_info",$param_p);
        // 二维码链接
        $url_arr['qr_admin']    = $wx_request_url . "?shop_id={$this->shop_id}&call_back={$admin_call_bacl_url}";
        $url_arr['qr_sign_in']  = $wx_request_url . "?shop_id={$this->shop_id}&call_back={$sign_call_bacl_url}";
        $url_arr['qr_teacher']  = $wx_request_url . "?shop_id={$this->shop_id}&call_back={$teacher_call_bacl_url}";
        $url_arr['qr_parent']  = $wx_request_url . "?shop_id={$this->shop_id}&call_back={$parent_call_bacl_url}";
//        $url_arr['qr_admin']    = $wx_request_url . "?shop_id=4&call_back={$admin_call_bacl_url}";
//        $url_arr['qr_sign_in']  = $wx_request_url . "?shop_id=4&call_back={$sign_call_bacl_url}";
//        $url_arr['qr_teacher']  = $wx_request_url . "?shop_id=4&call_back={$teacher_call_bacl_url}";

        //微信 js config
        $weixin = new Weixin($this->shop_id);
        $access_token = $weixin->getAccessToken();
          foreach ($url_arr as $k=>$v){
              if($v){
                  $json_data=json_encode(array("action"=>"long2short","long_url"=>$v));
                  $result = $this->create_short_url($access_token,$json_data);
                  $ur= $result['short_url'];
                  $url_arr1[]=$ur;

              }
          }

        $this->assign('url_arr',$url_arr1);

        return $this->fetch();
    }


    public  function create_short_url($accesss_token='',$json_data){
        $url = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=".$accesss_token;
        $weixin = new Weixin($this->shop_id);
        $result = $weixin->https_request($url,$json_data);
        $jsoninfo = json_decode($result,true);
        return $jsoninfo;
    }
    
}
